<template>
    <a-card hoverable>
        <a-form ref="formRef" :model="form" :rules="rules" :labelCol="{ span: 6 }">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="产品" name="product">
                        <a-select v-model:value="form.product" style="width: 100%" placeholder="请选择产品">
                            <a-select-option value="ActiveDirectory">ActiveDirectory</a-select-option>
                            <a-select-option value="OpenLDAP">OpenLDAP</a-select-option>
                            <a-select-option value="StandardLDAP">StandardLDAP</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="地址" name="providerUrl">
                        <a-input style="width: 100%" placeholder="请输入地址" v-model:value="form.providerUrl" />
                    </a-form-item>
                    <a-form-item label="账号" name="principal">
                        <a-input style="width: 100%" placeholder="请输入账号" v-model:value="form.principal" />
                    </a-form-item>
                    <a-form-item label="基本DN" name="basedn" v-if="form.product != 'ActiveDirectory'">
                        <a-input style="width: 100%" placeholder="请输入基本DN" v-model:value="form.basedn" />
                    </a-form-item>

                    <a-form-item label="AD域名" name="msadDomain" v-if="form.product == 'ActiveDirectory'">
                        <a-input style="width: 100%" placeholder="请输入AD域名" v-model:value="form.msadDomain" />
                    </a-form-item>
                    <a-form-item label="SSL" name="sslSwitch">
                        <a-switch v-model:checked="form.sslSwitch">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="证书" name="trustStore" v-if="form.sslSwitch">
                        <a-input style="width: 100%" placeholder="请输入证书" v-model:value="form.trustStore" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="状态" name="status">
                        <a-switch v-model:checked="form.status">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="域账号" name="accountMapping">
                        <a-switch v-model:checked="form.accountMapping">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="凭证" name="credentials">
                        <a-input-password style="width: 100%" placeholder="请输入凭证" v-model:value="form.credentials" />
                    </a-form-item>
                    <a-form-item label="过滤器" name="filters">
                        <a-input style="width: 100%" placeholder="请输入过滤器" v-model:value="form.filters" />
                    </a-form-item>
                    <a-form-item label="证书密钥" name="trustStorePassword" v-if="form.sslSwitch">
                        <a-input style="width: 100%" placeholder="请输入证书密钥" v-model:value="form.trustStorePassword" />
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
        <div style="text-align: center;">
            <a-button type="primary" @click="submit">提交</a-button>
            <a-button style="margin-left: 10px;">测试</a-button>
        </div>
    </a-card>
</template>

<script setup>
import { onMounted, reactive, ref, watch, createVNode } from 'vue';
import { ldapcontextApi } from '/src/api/security/ldap/api';
import { Modal, message } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import _ from 'lodash';
import { SmartLoading } from '/src/components/framework/smart-loading';
import { smartSentry } from '/src/lib/smart-sentry';


// ----------------------- 表单 ---------------------

const formRef = ref();


const formDefault = {
    id: undefined,
    product: undefined,
    providerUrl: undefined,
    principal: undefined,
    basedn: undefined,
    sslSwitch: 0,
    status: 0,
    accountMapping: false,
    credentials: undefined,
    filters: undefined,
    msadDomain: undefined,
    trustStore:undefined,
    trustStorePassword:undefined
};

let form = reactive({ ...formDefault });

// 表单规则
const rules = {
    product: [{ required: true, message: '请选择产品' }],
    providerUrl: [{ required: true, message: '请输入地址' }],
    principal: [{ required: true, message: '请输入账号' }],
    basedn: [{ required: true, message: '请输入基本DN' }],
    credentials: [{ required: true, message: '请输入凭证' }],
    accountMapping: [{ required: true, message: '请选择域账号' }],
    status: [{ required: true, message: '请选择状态' }],
    filters: [{ required: true, message: '请输入状态' }],
    msadDomain: [{ required: true, message: '请输入AD域名' }],
};



//

// ----------------------- 部门树的展示 ---------------------
async function submit() {
    form.accountMapping = form.accountMapping == true ? "YES" : "NO"
    form.status = form.status ? 1 : 0
    form.sslSwitch = form.sslSwitch ? "1" : "0"
    let res = await ldapcontextApi.updateldapcontext(form);
    if (res.code == 0) {
        message.success("修改成功")
        init();
    }
}

onMounted(() => {
    init();
});

// 
async function init() {
    try {

        let res = await ldapcontextApi.getldapcontext();
        if (res.code == 0) {
            form.id = res.data.id;
            form.product = res.data.product;
            form.providerUrl = res.data.providerUrl
            form.principal = res.data.principal
            form.basedn = res.data.basedn
            form.sslSwitch = res.data.sslSwitch
            form.status = res.data.status
            form.accountMapping = res.data.accountMapping
            form.credentials = res.data.credentials
            form.filters = res.data.filters
            form.msadDomain = res.data.msadDomain
            form.accountMapping = form.accountMapping == "YES" ? true : false
            form.status = form.status == 1 ? true : false
            form.sslSwitch = form.sslSwitch == "1" ? true : false
            form.trustStore =  res.data.trustStore
            form.trustStorePassword =  res.data.trustStorePassword
        }
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        tableLoading.value = false;
    }
}



// 重置
function resetQuery() {
    queryParam.value.providerName = ""
    queryParam.value.pageSize = 10
    queryParam.value.pageNumber = 1
    queryParam.value.total = 0
    onSearch();
}

// 搜索
function onSearch() {
    init();
}



// ----------------------- 表单操作：添加部门/修改部门/删除部门/上下移动 ---------------------


// 编辑
function updateRecord(e) {
    getRecordById(e);
    title.value = "编辑"
    visible.value = true
}

async function getRecordById(id) {
    let res = await socialsproviderApi.getSocialsProviderById(id);
    disabledCode.value = true
    if (res.code == 0) {
        form.id = res.data.id
        form.icon = res.data.icon
        form.provider = res.data.provider
        form.providerName = res.data.providerName
        form.clientId = res.data.clientId
        form.clientSecret = res.data.clientSecret
        form.agentId = res.data.agentId
        form.scanCode = res.data.scanCode
        form.display = res.data.display
        form.sortIndex = res.data.sortIndex
        form.status = res.data.status

    }

}

// 删除
function deleterRecord(id) {
    Modal.confirm({
        title: '提醒',
        icon: createVNode(ExclamationCircleOutlined),
        content: '确定要删除吗?',
        okText: '删除',
        okType: 'danger',
        async onOk() {
            SmartLoading.show();
            try {
                await socialsproviderApi.deleteSocialsProvider(id);
                init();
            } catch (error) {
                smartSentry.captureError(error);
            } finally {
                SmartLoading.hide();
            }
        },
        cancelText: '取消',
        onCancel() { },
    });
}
</script>

<style scoped lang="less"></style>